package com.oocl.sc.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.oocl.sc.dao.SorderDao;
import com.oocl.sc.util.DbUtil;

public class SorderDaoImpl implements SorderDao{

	public String loadSorderByAccount(String account) {
		String sql = "select menulist,to_char(ctime,'yyyy-MM-dd HH24:mi:ss') as nn from sorder where account=?  order by ctime desc";
		Connection conn = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		String result = "";		
		JSONArray ja = new JSONArray();
		try {
			conn = DbUtil.getConnection();
			st = conn.prepareStatement(sql);
			st.setString(1, account);
			rs = st.executeQuery();
			while(rs.next()) {
				JSONObject jo = new JSONObject();
				jo.put("menulist",rs.getString("menulist"));			
				jo.put("ctime", rs.getString("nn"));
				ja.add(jo);
			}
			result = ja.toString();
		} catch (SQLException e) {
			e.printStackTrace();		
		} finally {
			DbUtil.free(conn, st, rs);
		}
		return result;
	}

	public int addMenu(String account, String menulist) {
		String sql = "insert into sorder(oid,account,menulist,ctime) values(sorder_seq.Nextval,?,?,sysdate)";
		Connection conn = null;
		PreparedStatement st = null;
		int m = 0;
		try {
			conn = DbUtil.getConnection();
			st = conn.prepareStatement(sql);
			st.setString(1, account);
			st.setString(2, menulist);
			m = st.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.free(conn, st, null);
		}
		return m;
	}

}
